package com.wh.common.util;

import com.baomidou.mybatisplus.entity.TableInfo;
import com.baomidou.mybatisplus.mapper.AutoSqlInjector;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.session.Configuration;


public class SqlInjector extends  AutoSqlInjector {

    @Override
    public void inject(Configuration configuration, MapperBuilderAssistant builderAssistant, Class<?> mapperClass,
                       Class<?> modelClass, TableInfo table) {
        /* 添加一个自定义方法 */
        getSequences(mapperClass, modelClass, table);

    }

    public void getSequences(Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
        /* 执行 SQL ，动态 SQL 参考类 SqlMethod */
        String tableName = table.getTableName();
        int len = tableName.lastIndexOf(".");
        if(len > 0){
            String[] userAndTable = tableName.split("\\.");
            tableName = userAndTable[0]+".SEQ_"+userAndTable[1];
        }
        String sql = null;
        if(null != table.getKeyColumn() && !"".equals(table.getKeyColumn())){
            sql = "select "+tableName+".Nextval as "+table.getKeyColumn()+" from dual";
        }else{
            sql = "select "+tableName+".Nextval from dual";
        }

        /* mapper 接口方法名一致 */
        String method = "getSequences";
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
       /* Class<?> mapperClass, String id, SqlSource sqlSource, SqlCommandType sqlCommandType, Class<?> parameterClass, String resultMap, Class<?> resultType, KeyGenerator
        keyGenerator, String keyProperty, String keyColumn*/
      // this.addMappedStatement(mapperClass,table.getTableName(), sqlSource,SqlCommandType.SELECT, null, table.getResultMap(),Long.class, null,table.getKeyProperty(),table.getKeyColumn());
       //this.addMappedStatement(mapperClass, method, sqlSource, SqlCommandType.SELECT, Integer.class);
       this.addSelectMappedStatement(mapperClass,method, sqlSource, Long.class, table);
    }



/*

    protected void injectInsertOneSql(boolean selective, Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
        KeyGenerator keyGenerator = new NoKeyGenerator();
        StringBuilder fieldBuilder = new StringBuilder();
        StringBuilder placeholderBuilder = new StringBuilder();
        SqlMethod sqlMethod = selective?SqlMethod.INSERT_ONE:SqlMethod.INSERT_ONE_ALL_COLUMN;
        fieldBuilder.append("\n<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n");
        placeholderBuilder.append("\n<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n");
        String keyProperty = null;
        String keyColumn = null;
        OracleKeyGenerator s;
        if(StringUtils.isNotEmpty(table.getKeyProperty())) {
            if(table.getIdType() == IdType.AUTO) {
                keyGenerator = new Jdbc3KeyGenerator();
                keyProperty = table.getKeyProperty();
                keyColumn = table.getKeyColumn();
            } else if(null != table.getKeySequence()) {
                keyGenerator = TableInfoHelper.genKeyGenerator(table, this.builderAssistant, sqlMethod.getMethod(), this.languageDriver);
                keyProperty = table.getKeyProperty();
                keyColumn = table.getKeyColumn();
                fieldBuilder.append(table.getKeyColumn()).append(",");
                placeholderBuilder.append("#{").append(table.getKeyProperty()).append("},");
            } else {
                fieldBuilder.append(table.getKeyColumn()).append(",");
                placeholderBuilder.append("#{").append(table.getKeyProperty()).append("},");
            }
        }

        List<TableFieldInfo> fieldList = table.getFieldList();
        Iterator i$ = fieldList.iterator();

        while(i$.hasNext()) {
            TableFieldInfo fieldInfo = (TableFieldInfo)i$.next();
            if(selective) {
                fieldBuilder.append(this.convertIfTagIgnored(fieldInfo, false));
                fieldBuilder.append(fieldInfo.getColumn()).append(",");
                fieldBuilder.append(this.convertIfTagIgnored(fieldInfo, true));
                placeholderBuilder.append(this.convertIfTagIgnored(fieldInfo, false));
                placeholderBuilder.append("#{").append(fieldInfo.getEl()).append("},");
                placeholderBuilder.append(this.convertIfTagIgnored(fieldInfo, true));
            } else {
                fieldBuilder.append(fieldInfo.getColumn()).append(",");
                placeholderBuilder.append("#{").append(fieldInfo.getEl()).append("},");
            }
        }

        fieldBuilder.append("\n</trim>");
        placeholderBuilder.append("\n</trim>");
        String sql = String.format(sqlMethod.getSql(), new Object[]{table.getTableName(), fieldBuilder.toString(), placeholderBuilder.toString()});
        SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
        this.addInsertMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource, (KeyGenerator)keyGenerator, keyProperty, keyColumn);
    }
*/


}
